package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.TeacherComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.TeacherInfo;
import com.go.where.family.pojo.TeacherRelationInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.TeacherRelationService;
import com.go.where.family.service.TeacherService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.TeacherVo;
import com.google.gson.Gson;

@Component("teacherComponent")
public class TeacherComponentImpl implements TeacherComponent {

	private static final Logger log = Logger
			.getLogger(TeacherComponentImpl.class);
	private Gson gson;
	@Resource(name = "teacherService")
	private TeacherService teacherService;
	@Resource(name = "teacherRelationService")
	private TeacherRelationService teacherRelationService;

	public TeacherComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<TeacherInfo> queryList(String body, Head head) {
		TeacherVo vo = gson.fromJson(body, TeacherVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);

			List<TeacherInfo> list = teacherService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (TeacherInfo t : list) {
					String teacherId = t.getTeacherId();
					TeacherRelationInfo tri = new TeacherRelationInfo();
					tri.setTeacherId(teacherId);
					List<TeacherRelationInfo> courseList = teacherRelationService
							.queryByTeacherId(tri);
					if (courseList != null && !courseList.isEmpty()) {
						t.setCourseList(courseList);
					}
					if (StringUtils.isNotBlank(t.getHeaderPath())) {
						t.setHeaderPath(CommonUtil.getUrlPrefix()
								+ t.getHeaderPath());
					}
				}
				return list;
			} else {
				return new ArrayList<TeacherInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取教师列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public TeacherInfo queryTeacherDetail(String body, Head head) {
		TeacherVo vo = gson.fromJson(body, TeacherVo.class);
		if (vo != null) {
			String teacherId = vo.getTeacherId();
			TeacherInfo ti = teacherService.queryTeacherDetail(teacherId);
			if (ti != null) {
				return ti;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询教师详情信息时,没有查询到数据,教师ID：" + teacherId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取视频详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
